from datetime import datetime
from flask import Flask
from config import DevConfig
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config.from_object(DevConfig)
db = SQLAlchemy(app)


class User(db.Model):
    id = db.Column(db.Integer(), primary_key=True)
    username = db.Column(db.String(25), unique=True, index=True)
    trueName = db.Column(db.String(25))
    password = db.Column(db.String(32))
    idCard = db.Column(db.String(18), unique=True)
    socialNumber = db.Column(db.String(25), unique=True)
    create_date = db.Column(db.DateTime)

    def __init__(self, username, password, create_date=datetime.now()):
        self.username = username
        self.password = password
        self.create_date = create_date

    def __repr__(self):
        return "<User `{}`".format(self.username)


class Department(db.Model):
    # 科室表
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(50), unique=True)
    introduction = db.Column(db.String(255))
    create_date = db.Column(db.DateTime)

    def __init__(self, name, introduction=None, create_date=datetime.now()):
        self.name = name
        self.introduction = introduction
        self.create_date = create_date

    def __repr__(self):
        return "<Department `{}`".format(self.name)


tags = db.Table(
        'department_doctors',
        db.Column('department_id', db.Integer, db.ForeignKey('department.id')),
        db.Column('doctor_id', db.Integer, db.ForeignKey('doctor.id'))
    )


class Doctor(db.Model):
    # 医生表
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(50),index=True)
    photo = db.Column(db.String(255))
    introduction = db.Column(db.String(255))
    specialty = db.Column(db.String(255))
    worker = db.Column(db.String(255))
    notes = db.Column(db.String(255))
    create_date = db.Column(db.DateTime)
    tags = db.relationship(
        'Department',
        secondary = tags,
        backref = db.backref('doctor')
    )

    def __init__(self, name, photo, introduction, specialty, worker, notes, create_date=datetime.now()):
        self.name = name
        self.photo = photo
        self.introduction = introduction
        self.specialty = specialty
        self.worker = worker
        self.notes = notes
        self.create_date = create_date

    def __repr__(self):
        return "<Doctor `{}`".format(self.name)


class ReserveNotes(db.Model):
    id = db.Column(db.Integer(), primary_key=True)
    department = db.Column(db.String(50))
    reserve_date = db.Column(db.Date)
    how_reserve = db.Column(db.String(25))
    state = db.Column(db.String(25))
    money = db.Column(db.Integer())
    create_date = db.Column(db.DateTime)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    doctor_id = db.Column(db.Integer, db.ForeignKey('doctor.id'))

    def __init__(self, department, reserve_date, how_reserve, state, money, user_id, doctor_id, create_date=datetime.now()):
        self.department = department
        self.reserve_date = reserve_date
        self.how_reserve = how_reserve
        self.state = state
        self.money = money
        self.user_id = user_id
        self.doctor_id = doctor_id
        self.create_date = create_date

    def __repr__(self):
        return "<ReserveNotes `{}`>".format(self.department)